/*
 * ODO: Real-Time In-Memory ORM/DBMS Grid software
 * Copyright (c) 2008, Michael Oksenenko
 * All rights reserved.
 */
//----------------------------------------------------------------------------
#include "OsConfig.hpp"
#if ODO_OS_VISTA == ODO_CHOSEN_OS
//----------------------------------------------------------------------------
#include "ErrorVista.hpp"

#include "windows.h"
#include <iostream>

using namespace odo;

void ErrorVista::printLast(unsigned long aErrorCode) {
	if (ERROR_SUCCESS == aErrorCode) return;
	const size_t MESSAGE_MAX_SIZE = static_cast<size_t>(STRING_BUFFER_SIZE);
	wchar_t errorMessage[1 + MESSAGE_MAX_SIZE];
	if (0 == FormatMessageW(
		FORMAT_MESSAGE_FROM_SYSTEM |
		FORMAT_MESSAGE_IGNORE_INSERTS,		// formatting options
		0,									// location of the message
		static_cast<DWORD>(aErrorCode),		// message identifier 
		0,									// language identifier
		static_cast<LPWSTR>(errorMessage),	// formatted message
		static_cast<DWORD>(MESSAGE_MAX_SIZE),// size of the out buffer
		0									// insert values 
	)) {
		std::wcerr
			<< L"FAIL (" << GetLastError() << L") "
			<< L"format last error message (" << aErrorCode << L")"
			<< std::endl;
	}
	else {
		errorMessage[MESSAGE_MAX_SIZE] = L'\0'; // guard
		std::wcerr << L"LAST_ERROR: " << errorMessage;
	}
}

unsigned long ErrorVista::printLast() { // return last error code
	unsigned long errorCode = static_cast<unsigned long>(GetLastError());
	printLast(errorCode);
	return errorCode;
}

ErrorVista::~ErrorVista() {
	// void
}

ErrorVista::ErrorVista() {
	// void
}
//----------------------------------------------------------------------------
#endif /* ODO_OS_VISTA */
//----------------------------------------------------------------------------
